/**
 * 接口请求封装
 * 支持 get post
 */
 import axios from 'axios'
 import { Message  } from 'element-ui'
 import config from './host'
 axios.defaults.baseURL= config.baseUrl
  const request = (method = '', url = '', data = {} ,params = {}) => {
    return new Promise((resolve, reject) => {
     axios.request({
          url,
          method,
          params,
          headers: {
              'Content-Type': 'application/json; charset=UTF-8',
              'Authorization': sessionStorage.getItem('token')   
          },
          data
        }).then( res => {
             const { status } = res
             if (status === 200) {
               const { errCode, errMsg } = res.data
               if (errCode !== 10000) {
                Message({
                  type: 'error',
                  message: errMsg,
                })
               }
               resolve(res.data)
             } else {
               // 可以用switch根据code判断显示不同的信息提示
               let msg = statusCode == 404 ? '路径有误' : '信息有误'
              Message({
                 type: 'error',
                 message: msg,
               })
             }
          }).catch( err => {
            reject(err)
          })
    })
  }
  
  
  const get = (url, params) => request('GET',url, null,params)
  const post = (url, data) => request('POST', url, data,null)
  export default {
     get,
     post
   }